package com.zhz.project.main.socolar.wenzhang;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhz.project.common.util.alibaba.StringUtil;
import com.zhz.project.common.util.ibatispage.IbatisPage;
import com.zhz.project.common.util.result.Result;
import com.zhz.project.common.util.result.ResultSupport;
import com.zhz.project.dal.daointerface.WenzhangDAO;
import com.zhz.project.dal.dataobject.WenzhangDO;
import com.zhz.project.main.socolar.wenzhang.util.FullTextUrlParser;
import com.zhz.project.main.socolar.wenzhang.util.GetCookieKey;
import com.zhz.project.util.getWebContent;

public class SocolarFullTextUrlMain implements Runnable {
	private static Log logger = LogFactory.getLog(SocolarFullTextUrlMain.class);

	public static String url = "http://www.socolar.com/";

	public int pageNo;

	public SocolarFullTextUrlMain(int pageNo) {
		this.pageNo = pageNo;
	}

	public void run() {
		init(pageNo);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void init(int pageNo) {
		System.out.println("begin");

		ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
				new String[] { "ibatis-spring.xml" });

		// 文章DAO
		WenzhangDAO wenzhangDAO = (WenzhangDAO) appContext
				.getBean("wenzhangDAO");

		// 查詢需要處理的卷期
		IbatisPage page = (IbatisPage) appContext.getBean("ibatisPage");

		Map map = new HashMap();
		map.put("indexPage", pageNo + "");
		Map result = page.getPage(map, "selectWenzhangCount",
				"selectWenzhangByPage");
		List<WenzhangDO> wenzhangDOlist = (List<WenzhangDO>) result.get("list");

		// WenzhangDO wenzhangDO2 = wenzhangDOlist.get(0);
		// logger.info("size=" + wenzhangDOlist.size() + "==thread+" +
		// Thread.currentThread().getId()
		// + "  pageNo=" + pageNo + "==" + "id=" + wenzhangDO2.getId());
		// if(true)
		// return;
		String key = null;
		String cookie = null;

		Result codeResult = new ResultSupport(false);
		System.out.println("GetCookieKey.verify()");

		do {
			codeResult = GetCookieKey.verify();
		} while (!codeResult.isSuccess());

		key = (String) codeResult.getModels().get("key");
		cookie = (String) codeResult.getModels().get("cookie");

		// logger.info("key=" + key);
		// logger.info("cookie=" + cookie);
		System.out.println("wenzhangDOlist,size=" + wenzhangDOlist.size());

		for (WenzhangDO wenzhangDO : wenzhangDOlist) {
			try {
				if (StringUtil.isBlank(wenzhangDO.getFulltexturl())) {
					System.out.println("ignore=" + wenzhangDO.getId());
					continue;
				}
				// Thread.sleep(1000);
				String fulltextUrl = parseFullText(
						url + wenzhangDO.getFulltexturl() + "&key=" + key,
						cookie);
				if (StringUtil.isNotBlank(fulltextUrl)) {
					// logger.info("thread=" + Thread.currentThread().getId() +
					// "  pageNo=" + pageNo);
					Date date = new Date();
					System.out.println(date + "  id=" + wenzhangDO.getId()
							+ " fulltextUrl=" + fulltextUrl);
					// logger.info("id=" + wenzhangDO.getId() + " fulltextUrl="
					// + fulltextUrl);
					wenzhangDAO.updateExone(fulltextUrl, wenzhangDO.getId());
					wenzhangDAO.updateExtwo("true", wenzhangDO.getId());
				} else {
					wenzhangDAO.updateExtwo("wait", wenzhangDO.getId());

					System.out.println("-----------" + wenzhangDO.getId()
							+ "---reget key&cookie--------");
					// logger.info("-----------" + wenzhangDO.getId() +
					// "---reget key&cookie--------");
					do {
						codeResult = GetCookieKey.verify();
					} while (!codeResult.isSuccess());

					key = (String) codeResult.getModels().get("key");
					cookie = (String) codeResult.getModels().get("cookie");
				}

			} catch (Exception e) {
				logger.info("--------------------Exception ,continue--------------------");
			}
		}

	}

	/**
	 * 根据URL获取全文链接
	 * 
	 * @param url
	 * @param cookie
	 * @return
	 */
	public String parseFullText(String url, String cookie) {

		String content = getWebContent.getSocolarContentHttpURLConnection(url,
				cookie, false);

		FullTextUrlParser parser = new FullTextUrlParser();

		parser.setContent(content);

		return parser.parse();
	}

}
